package com.usian.service.impl;

import com.usian.config.RedisClient;
import com.usian.service.CartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Value("${CART_REDIS_KEY}")
    private String CART_REDIS_KEY;

    @Autowired
    private RedisClient redisClient;


    @Override
    public Map<String, Object> getCartByUserId(String userId) {
        Map<String,Object> cartRedis = (Map<String,Object>)redisClient.hget(CART_REDIS_KEY, userId);
        if(cartRedis !=null){
            return cartRedis;
        }
        return new HashMap<String, Object>();
    }

    @Override
    public boolean addCartToRedis(String userId, Map<String, Object> cart) {
        boolean result = redisClient.hset(CART_REDIS_KEY, userId, cart);
        return result;
    }
}
